package com.fcscs.cservice.client.main.restaurants;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.common.MainPaginationSearchModel;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.SearchExecutor;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.controller.BookingServiceController;
import com.fcscs.cservice.client.common.model.restaurants.BookingViewModelData;
import com.fcscs.cservice.domain.model.BookingUpdate;
import com.fcscs.cservice.domain.model.BookingView;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class GuestDetailGrid extends
		PagingSearchResultsPanel<BookingViewModelData, MainPaginationSearchModel> implements
		FormSubmitListener<BookingUpdate>, ModelSelectListener<BookingViewModelData> {

	private ResBookingDetailDialog bookingDetailDialog;
	private BookingViewModelData currentModelData;

	public GuestDetailGrid(String title, SearchExecutor<BookingViewModelData, MainPaginationSearchModel> exe) {
		super(title, exe, null, (Object[]) null);
		registerListeners(this);
		setSize(960, 400);
	}
	
	@Override
	public void onModelClick(BookingViewModelData model) {
	}

	@Override
	public void onModelDoubleClick(BookingViewModelData model) {
		
	}

	@Override
	public void onModelEnterPress(BookingViewModelData model) {
		
	}


	@Override
	protected List<ColumnConfig> getColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig cc = CoreUtil.createNonSortableColumnConf(CServiceConstants.RESTAURANT,
				CServiceConstants.CSERVICE_CONSTANTS.restaurant(), 180);
		columns.add(cc);
		
		cc = CoreUtil.createNonSortableColumnConf(CServiceConstants.GUESTNAME,
				CServiceConstants.CSERVICE_CONSTANTS.guestName(), 170);
		columns.add(cc);

		columns.add(CoreUtil.createNonSortableColumnConf(ShareGWTConstants.DATE,
				CServiceConstants.CSERVICE_CONSTANTS.dateBookFor(), 180));
		// columns.add(CoreUtil.createNonSortableColumnConf(ShareGWTConstants.TIME,
		// CoreConstants.CORE_CONSTANTS.time(), 150));
		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.BOOKINGSTATUS,
				CServiceConstants.CSERVICE_CONSTANTS.bookingStatus(), 160));
		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.GUESTTYPE,
				CServiceConstants.CSERVICE_CONSTANTS.guestType(), 160));
		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,
				CoreConstants.CORE_CONSTANTS.action(), 80);

		cc.setRenderer(new GridCellRenderer<BookingViewModelData>() {

			@Override
			public Object render(BookingViewModelData model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<BookingViewModelData> store,
					Grid<BookingViewModelData> grid) {
				HorizontalPanel hr = new HorizontalPanel();
				hr.setHorizontalAlign(HorizontalAlignment.LEFT);

				HighLightIconButton ib = ShowGuestBookingHistory(model);
				ib.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
				ib.setId("ServiceItemPriorityGridPanel_edit_button");
				hr.add(ib);
				return hr;
			}
		});
		columns.add(cc);
		return columns;
	}

	public HighLightIconButton ShowGuestBookingHistory(final BookingViewModelData model) {
		HighLightIconButton iconBtn = new HighLightIconButton(ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {

					@Override
					public void componentSelected(IconButtonEvent ce) {
						currentModelData = model;
						bookingDetailDialog = ResBookingDetailDialog.getInstance(GuestDetailGrid.this,model);
						bookingDetailDialog.show();
					}
				});

		return iconBtn;
	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<BookingUpdate> form, SubmitType submitType) {
		BookingUpdate bUpdate = new BookingUpdate();
		bookingDetailDialog.getFormPanel().populateEntity(bUpdate);
		updateBookingUpdate(bUpdate);
	}
	
	public void updateBookingUpdate(final BookingUpdate bu){
		BookingServiceController.getInstance().updateBooking(bu,
				new AsyncCallbackListener<BookingView>() {

					@Override
					public void onLogin(String username) {
						BookingServiceController.getInstance().updateBooking(bu, this);
					}

					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
								CoreConstants.CORE_CONSTANTS.system(),
								ShareGWTConstants.SHARE_GWT_CONSTANTS.error()), CoreConstants.CORE_CONSTANTS
								.systemError(), null);	
					}

					@Override
					public void onSuccess(BookingView result) {
						if (null != result) {
							replaceModelData(currentModelData, new BookingViewModelData(result));
							bookingDetailDialog.hide();
							CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.saved());
						}
					}
				});
	}
}
